<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Login Nav
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  Logout Case
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Transform member into guest
		//-----------------------------------------------------------------

		$update_guest = $db->getLine("SELECT * FROM online WHERE sid = '$session_id' AND type = 'live'");
		$db->updateRow("online",array('type' => 'live','status' => 'guest','user' => NULL,'user_id' => NULL,'user_authorization' => NULL,'user_email' => NULL,'user_hideemail' => NULL,'user_flag' => NULL,'user_location' => NULL,'refer' => ''.$_SERVER['HTTP_REFERER'].'','date' => ''.time().''),"id = {$update_guest['id']}");

		//-----------------------------------------------------------------
		//  Unregister the sesions and destroy cookies
		//-----------------------------------------------------------------

		session_unregister('id');
		session_unregister('authorization');
		session_unregister('username');
		session_unregister('lastvisit');
		session_unregister('forum_lastvisit');

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?i=4");

	break;

	//-----------------------------------------------------------------
	//  Login Case
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Encode the password and look for a match in the database
		//-----------------------------------------------------------------

		$tmp = $db->getLine("SELECT * FROM users WHERE username = '".addslashes($username)."' AND password = '".md5($password)."'");

		//-----------------------------------------------------------------
		//  If a match is found login else redirect with error message
		//-----------------------------------------------------------------

		if($tmp != FALSE)
		{
			//-----------------------------------------------------------------
			//  If the user has not been activated show error
			//-----------------------------------------------------------------

			if($tmp['status'] == "activate"){ header("location: $PHP_SELF?i=17"); exit; }

			//-----------------------------------------------------------------
			//  If the user is banned and invizible
			//-----------------------------------------------------------------

			if($tmp['status'] == "banned")
			{
				//-----------------------------------------------------------------
				//  Add the detalies into sessions and cookies
				//-----------------------------------------------------------------

				$_SESSION['id'] = $tmp['id'];
				$_SESSION['authorization'] = $tmp['authorization'];
				$_SESSION['username'] = $tmp['username'];

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/hq&i=3");
			}

			//-----------------------------------------------------------------
			//  If the user is ready or in forgot he can login
			//-----------------------------------------------------------------

			if($tmp['status'] == "ready" || $tmp['status'] == "forgot")
			{
				//-----------------------------------------------------------------
				//  Check see if he is allready loged in
				//-----------------------------------------------------------------

				$checker = $db->getLine("SELECT * FROM online WHERE type = 'live' AND user = '{$tmp['username']}' AND user_id = '{$tmp['id']}'");
				if($checker != FALSE && !isset($d) && !isset($o)) { header("location: $PHP_SELF?i=5"); exit; }

				//-----------------------------------------------------------------
				//  Add the detalies into sessions and cookies
				//-----------------------------------------------------------------

				$_SESSION['id'] = $tmp['id'];
				$_SESSION['authorization'] = $tmp['authorization'];
				$_SESSION['username'] = $tmp['username'];
				$_SESSION['lastvisit'] = $tmp['lastvisit'];
				$_SESSION['forum_lastvisit'] = $tmp['forum_lastvisit'];

				//-----------------------------------------------------------------
				//  Update last visit
				//-----------------------------------------------------------------

				$tmp['lastvisit'] = time();
				$tmp['forum_lastvisit'] = time();
				$db->updateRow("users",$tmp,"id={$tmp['id']}");

				//-----------------------------------------------------------------
				//  Transform guest into member
				//-----------------------------------------------------------------

				$update_member = $db->getLine("SELECT * FROM online WHERE sid = '$session_id' AND type = 'live'");
				$db->updateRow("online",array('type' => 'live','status' => 'member','user' => ''.$tmp['username'].'','user_id' => ''.$tmp['id'].'','user_authorization' => ''.$tmp['authorization'].'','user_email' => ''.$tmp['email'].'','user_hideemail' => ''.$tmp['hideemail'].'','user_flag' => ''.$tmp['flag'].'', 'user_location' => ''.$tmp['location'].'','refer' => ''.$_SERVER['HTTP_REFERER'].'','date' => ''.time().''),"id = {$update_member['id']}");

				//-----------------------------------------------------------------
				//  Insert online log
				//-----------------------------------------------------------------

				$lang = @explode (",", @getenv("HTTP_ACCEPT_LANGUAGE"));
				$lang = @strtolower ($lang[0]);

				$db->insertRow("online",array('sid' => ''.$session_id.'','type' => 'log','status' => 'member','user' => ''.$_SESSION['username'].'','user_id' => ''.$_SESSION['id'].'','user_authorization' => ''.$tmp['authorization'].'','user_email' => ''.$tmp['email'].'','user_hideemail' => ''.$tmp['hideemail'].'','user_flag' => ''.$tmp['flag'].'','user_location' => ''.$tmp['location'].'','ip' => ''.find_ip().'','refer' => ''.$_SERVER['HTTP_REFERER'].'','browser' => ''.find_browser($_SERVER['HTTP_USER_AGENT']).'','lang' => ''.$lang.'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				if($config['maintance'] == 1 && $tmp['authorization'] != 4) header("location: $PHP_SELF");
				else header("location: $PHP_SELF?n=modules/hq&i=3");
			}
		}
		else header("location: $PHP_SELF?i=1");

	break;
}

?>